package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.UserLike;
import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import javax.naming.NamingEnumeration;
import java.util.Date;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Long likeUserId) {
        if (!this.exists(userId, likeUserId)) {
            UserLike userLike = new UserLike();
            userLike.setId(new ObjectId());
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(new Date().getTime());
            mongoTemplate.save(userLike);
        }

    }

    @Override
    public boolean exists(Long userId, Long likeUserId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        return mongoTemplate.exists(query, UserLike.class);
    }

    @Override
    public Integer queryLoveCounts(Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        Integer loveCount = userLikes.size();
        return loveCount;
    }

    @Override
    public Integer queryFanCounts(Long userId) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        Integer fanCount = userLikes.size();
        return fanCount;
    }


    @Override
    public PageResult queryContacts(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("likeUserId").is(userId));
        query.limit(pagesize).skip((page - 1) * pagesize);
        query.with(Sort.by(Sort.Direction.DESC, "created"));

        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult(page, pagesize, (int) count, userLikeList);

    }

    @Override
    public PageResult queryToLike(Long userId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("userId").is(userId));
        query.limit(pagesize).skip((page - 1) * pagesize);
        query.with(Sort.by(Sort.Direction.DESC, "created"));

        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        return new PageResult(page, pagesize, (int) count, userLikeList);
    }


    @Override
    public boolean findLike(Long userId, Long deleteId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(deleteId));
        return mongoTemplate.exists(query, UserLike.class);
    }


    @Override
    public void deleteLike(Long userId, Long deleteId) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(deleteId));
        mongoTemplate.remove(query, UserLike.class);
    }


    @Override
    public boolean queryLike(Long userId, Long likeUserId1) {
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId1));
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        return exists;
    }


}
